#!/bin/sh

# fail if any commands fails
set -e
# debug log
#set -x

show_usage (){
    printf "Usage: $0 [options [parameters]]\n"
    printf "\n"
    printf "Mandatory options:\n"
    printf " -g|--game          [doom|doom2|tnt|plutonia|heretic|hexen|chex3]\n"
    printf " -e|--engine        [chocolate|crispy|prboom|dsda|gzdoom]\n"
    printf " -l|--map_limit     [none|vanilla|nolimit|boom|zdoom]\n"
    printf "\n"
    printf "Options:\n"
    printf " -d|--game-dir      [/path/to/doom/base/directory] (Optional, default: '~/games/doom')\n"
    printf " -s|--skill         [1, 2, 3, 4, 5] (Optional, default: '3')\n"
    printf " -r|--map_generator [none|slige|obsidian] (Optional, default: 'none')\n"
    printf " -m|--mods          [none|vanilla|improved|beautiful|brutal|samsara] (Optional, default: 'vanilla')\n"
    printf " -u|--mangohud      [true|false] (Optional, default: 'no')\n"
    printf "    --nomo          no monsters [true|false] (Optional, default: 'false')\n"
    printf " -h|--help, Print help\n"

exit
}

if [ "$1" = "--help" ] || [ "$1" = "-h" ]; then
  show_usage
fi
if [ -z "$1" ]; then
  show_usage
fi

while [ ! -z "$1" ]; do
  case "$1" in
     --game-dir|-d)
        shift
        echo "INFO: param - game directory: $1"
        param_game_dir=$1
        ;;
     --game|-g)
        shift
        echo "INFO: param - iwad: $1"
        param_iwad=$1
        ;;
     --engine|-e)
        shift
        echo "INFO: param - engine: $1"
        param_engine=$1
        ;;
     --map_limit|-l)
        shift
        echo "INFO: param - map limit $1"
        param_map_limit=$1
         ;;
     --skill|-s)
        shift
        echo "INFO: param - skill: $1"
        param_skill=$1
         ;;
     --map_generator|-r)
        shift
        echo "INFO: param - map generator: $1"
        param_map_generator=$1
         ;;
     --nomo)
        shift
        echo "INFO: param - no monsters: $1"
        param_nomo=$1
         ;;
     --mods|-m)
        shift
        echo "INFO: param - mods: $1"
        param_mods=$1
         ;;
     --mangohud|-u)
        shift
        echo "INFO: param - mangohud: $1"
        param_mangohud=$1
         ;;
     *)
        show_usage
        ;;
  esac
shift
done

### Configuration
if [ -z "$param_game_dir" ]; then
      param_game_dir=~/games/doom
fi
config_script_dir="$(pwd $(dirname $0))"
config_iwads_dir=$param_game_dir/wads/iwads
if [ -z "$param_engine" ]; then
      echo "ERROR: 'engine' parameter is mandatory"
fi
if [ -z "$param_map_generator" ]; then
      param_map_generator=none
fi
if [ -z "$param_skill" ]; then
      param_skill=3
fi
if [ -z "$param_mods" ]; then
      param_mods=vanilla
fi
if [ -z "$param_nomo" ]; then
      param_mods=false
fi
if [ -z "$param_mangohud" ]; then
      param_mangohud=false
fi

### check parameter values
# iwad
games="doom doom2 tnt plutonia heretic hexen chex3"
echo "$games" | tr ' ' '\n' | while read -r item; do
  if [ "$item" = "$param_iwad" ]; then touch match; fi
done
if [ ! -f match ]; then echo "ERROR: $param_iwad is not a valid option, valid options are: $games"; exit 1; fi; rm match

map_limit="none vanilla nolimit boom zdoom"
echo "$map_limit" | tr ' ' '\n' | while read -r item; do
  if [ "$item" = "$param_map_limit" ]; then touch match; fi
done
if [ ! -f match ]; then echo "ERROR: $param_map_limit is not a valid option, valid options are: $map_limit"; exit 1; fi; rm match

map_generators="none slige obsidian"
echo "$map_generators" | tr ' ' '\n' | while read -r item; do
  if [ "$item" = "$param_map_generator" ]; then touch match; fi
done
if [ ! -f match ]; then echo "ERROR: $param_map_generator is not a valid option, valid options are: $map_generators"; exit 1; fi; rm match

mods="none vanilla improved beautiful brutal samsara"
echo "$mods" | tr ' ' '\n' | while read -r item; do
  if [ "$item" = "$param_mods" ]; then touch match; fi
done
if [ ! -f match ]; then echo "ERROR: $param_mods is not a valid option, valid options are: $mods"; exit 1; fi; rm match

skills="1 2 3 4 5"
echo "$skills" | tr ' ' '\n' | while read -r item; do
  if [ "$item" = "$param_skill" ]; then touch match; fi
done
if [ ! -f match ]; then echo "ERROR: $param_skill is not a valid option, valid options are: $skills"; exit 1; fi; rm match

